/**
 * 表单验证
 * @param form 需要验证表单的jquery对象
 * @param successCallBack 提交成功后的回调方法
 * @returns {null} 初始化验证后的表单对象
 * @param submitBefore 提交表单之前的验证
 * @constructor
 */
var ClientValidatorForm = function (form, successCallBack, submitBefore) {
    var that = this;
    that.form = form;
    that.successCallBack = successCallBack;
    that.submitBefore = submitBefore;
    /**
     * 初始化表单验证
     * @param callback 初始化后的会掉函数
     * @param isUseRequesetBody 是否使用requestBody的形式发送请求
     * @param getExtendRequestBodyParams 获取requestBodyc参数值
     * @returns {null} 当前被初始化过后的表单jquery对象
     */
    that.init = function (callback, isUseRequesetBody, getExtendRequestBodyParams) {
        if (!form && form.size() != 0) {
            return null;
        }
        var initForm = form.bootstrapValidator().on('success.form.bv', function (e) {
            var form = $(e.target);
            if (typeof that.submitBefore === "function") {
                // 提交之前的验证
                if (!that.submitBefore()) {
                    return false;
                }
            }
            var addUrlApiKey = form.attr("addUrlApiKey");
            var updateUrlApiKey = form.attr("updateUrlApiKey");
            var submitUrlApiKey = addUrlApiKey;
            if (form.find("[name='id']").val()) {
                submitUrlApiKey = updateUrlApiKey;
            }
            var method = form.attr("method");
            var responseType = form.attr("responseType");
            if (!submitUrlApiKey) {
                logger.error("The submit path map is not set");
                return false;
            }
            if (!method) {
                method = "POST";
            }
            if (!responseType) {
                responseType = "html";
            } else {
                responseType = responseType.toLowerCase();
            }
            var params = {};
            if (isUseRequesetBody == true) {//使用形式requestbody请求
                var requestBody = {};
                $.each(form.serializeArray(), function (key, val) {
                    requestBody[val.name] = val.value;
                });
                //将登陆者id放入requestBody中
                requestBody.loginAdminUserId = loginAdminUserId;

                if (requestBody && requestBody["feesName"] && requestBody["discountedMoney"] && requestBody["practicalMoney"] && requestBody["standardMoney"]) {
                    delete requestBody.feesName;
                    delete requestBody.discountedMoney;
                    delete requestBody.practicalMoney;
                    delete requestBody.standardMoney;
                }

                if (typeof  getExtendRequestBodyParams === "function") {
                    //获取requestBody参数
                    var extendRequestBodyParams = getExtendRequestBodyParams();
                    //将扩展参数添加到requestBody
                    $.each(extendRequestBodyParams, function (key, val) {
                        requestBody[key] = val;
                    })
                }
                params = JSON.stringify(requestBody)

            } else {
                params = form.serializeArray();
            }
            Http.request(PlatformAPI.getUrl(submitUrlApiKey) + (isUseRequesetBody ? "?useRequestBody=true" : ""), params, function (resultText) {
                try {
                    var result = responseType == "json" ? resultText : eval("(" + resultText + ")");
                    if (typeof that.successCallBack === "function") {
                        that.successCallBack(result);
                    }
                } catch (e) {
                    console.log(resultText);
                    console.log(e);
                    logger.error("请求结果解析异常:")
                }
            }, function (XMLHttpRequest, textStatus, errorThrown) {
                logger.info("request status " + textStatus)
                if (errorThrown) {
                    logger.info("request thrown " + errorThrown)
                }
                var responseText = XMLHttpRequest.responseText;
                logger.error("请求异常:" + responseText);
            }, method, responseType, null, isUseRequesetBody == true ? "application/json" : null);
            return false;
        });
        if (typeof  callback === "function") {
            callback();
        }
        return initForm;
    }
}